﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DB;

namespace BookManagement
{
    public partial class Form1 : Form
    {

        private int dataRow = 0;

        public Form1()
        {
            InitializeComponent();

        }

        /// <summary>
        /// ロード時処理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            DateTime dt=DateTime.Today;

            //GetDatatable();
            this.dgvBookList.DataSource = GetDatatable();

            //if (this.dgvBookList.Rows.Count == 0) return;

            //for(int i=0; i<this.dgvBookList.Rows.Count;i++){

            //    if ((DateTime)this.dgvBookList["ClmReleaseDay", i].Value == dt)
            //    {
            //        this.label1.Text = "にーこにっこどうが";
            //    }
            //}
            dataRow = this.dgvBookList.Rows.Count - 1;

        }

        private void btnBookEdit_Click(object sender, EventArgs e)
        {
            string title = this.textBox1.Text;
            string authorName = this.textBox2.Text;
            string genre = this.textBox3.Text;
            string releaseDay = this.textBox4.Text;
            string price = this.textBox5.Text;

            Dictionary<string, object> paramDic = new Dictionary<string, object>();
            paramDic.Add("TITLE", title);
            paramDic.Add("AUTHORNAME", authorName);
            paramDic.Add("GENRE", genre);
            paramDic.Add("RELESE", releaseDay);
            paramDic.Add("PRICE", price);

            DBLogic.ExecuteNonQuery("DB.SQL.Insert_TBOOKMANAGER.sql", paramDic);


            this.dgvBookList.DataSource = GetDatatable();
            

        }





        /// <summary>
        /// 更新処理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void update_Click(object sender, EventArgs e)
        {

            Dictionary<string, object> paramDic = new Dictionary<string, object>();

            //更新データ取得
            paramDic.Add("TITLE", this.dgvBookList["ClmTitle", this.dgvBookList.CurrentCell.RowIndex].Value.ToString());
            paramDic.Add("AUTHORNAME", this.dgvBookList["ClmAuthorName", this.dgvBookList.CurrentCell.RowIndex].Value.ToString());
            paramDic.Add("GENRE", this.dgvBookList["ClmGenre", this.dgvBookList.CurrentCell.RowIndex].Value.ToString());
            paramDic.Add("RELESE", this.dgvBookList["ClmReleaseDay", this.dgvBookList.CurrentCell.RowIndex].Value.ToString());
            paramDic.Add("PRICE", this.dgvBookList["ClmPrice", this.dgvBookList.CurrentCell.RowIndex].Value.ToString());

            //更新したID取得
            paramDic.Add("ID", this.dgvBookList["ID", this.dgvBookList.CurrentCell.RowIndex].Value.ToString());

            //更新SQL処理
            DBLogic.ExecuteNonQuery("DB.SQL.Update_TBOOKMANAGER.sql", paramDic);


            ////SQLファイルリスト
            //List<string> sqlFile = new List<string>();
            //sqlFile.Add()



            //DBLogic.ExecuteNonQuery();


            //データ再格納
            BindingSource dt = GetDatatable();
            this.dgvBookList.DataSource = dt;

        }

        /// <summary>
        /// データテーブル設定
        /// </summary>
        /// <returns></returns>
        private BindingSource GetDatatable()
        {

            // DBのTBOOKMANAGERのカラム
            string[] column = new string[] { "ID", "TITLE", "AUTHORNAME", "GENRE", "RELESE", "PRICE" };
            // DBからTBOOKMANAGERのレコード取得
            var table = DBLogic.ExecuteReader("DB.SQL.Select_TBOOKMANAGER.sql", column);

            var bs = new BindingSource();

            // DBからとってきたレコードを順番に回す
            foreach (DataRow item in table.Rows)
            {
                // 各カラムの値を格納
                int id;
                int.TryParse(item["ID"].ToString(), out id);
                string title = (string)item["TITLE"];
                string authorName = (string)item["AUTHORNAME"];    // 著者名
                string genre = (string)item["GENRE"];
                DateTime dt;
                DateTime.TryParse(item["RELESE"].ToString(), out dt);
                int price;
                int.TryParse(item["PRICE"].ToString(), out price);

                var bookdate = new BookData()
                {
                    ClmID = id,
                    ClmTitle = title,
                    ClmAuthorName = authorName,
                    ClmGenre = genre,
                    ClmReleaseDay = dt,
                    ClmPrice = price

                };

                // BindingSourceにBookDate情報を追加
                bs.Add(bookdate);

            }

            return bs;
            // データグリッドに表示データを設定
            //this.dgvBookList.DataSource = bs;
            
        }



        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, object> paramDic = new Dictionary<string, object>();

            paramDic.Add("ID", this.dgvBookList["ID", this.dgvBookList.CurrentCell.RowIndex].Value.ToString());

            DBLogic.ExecuteNonQuery("DB.SQL.Delete_TBOOKMANAGER.sql", paramDic);

            BindingSource dt = GetDatatable();
            this.dgvBookList.DataSource = dt;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {



        }





 
    }
}
